import json
from collections import namedtuple

# 定义一个命名元组作为返回结果的类别
ResponseResult = namedtuple('ResponseResult', ['success', 'data', 'error_message'])


class ChatCompletion:
    def __init__(self, id, object, created, result, is_truncated, need_clear_history, finish_reason, usage):
        self.id = id
        self.object = object
        self.created = created
        self.result = result
        self.is_truncated = is_truncated
        self.need_clear_history = need_clear_history
        self.finish_reason = finish_reason
        self.usage = usage


def parse_chat_completion(json_str):
    data = json.loads(json_str)
    return ChatCompletion(
        id=data['id'],
        object=data['object'],
        created=data['created'],
        result=data['result'],
        is_truncated=data['is_truncated'],
        need_clear_history=data['need_clear_history'],
        finish_reason=data['finish_reason'],
        usage=data['usage']
    )
